<?php
/*
 * @Descripttion: 第三方开放接口查询token
 * @version: v1
 * @Author: Terry
 * @Date: 2021-10-11 12:20:07
 * @LastEditors: Terry
 * @LastEditTime: 2024-10-12 09:38:45
 */
declare (strict_types = 1);

namespace app\openApi\middleware;

use think\facade\Db;

class CheckToken
{
    private $errorcode = [
        'dataError' =>  -1, //参数有误
        'appidError'=>  -2, //appid错误
        'signError' =>  -3, //签名校验错误
        'timeOut' =>  -4, //超过约定时间
        'ruleError'=>  -5, //appid错误
    ];
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next){
        $data = $request->post();
        if(empty($data['appId'])) return result($this->errorcode['dataError'],'','appid为空');
        if(empty($data['timestamp'])) return result($this->errorcode['dataError'],'','时间戳为空');
        if(empty($data['data'])) return result($this->errorcode['dataError'],'','data为空');
        if(empty($data['sign'])) return result($this->errorcode['dataError'],'','签名为空');
        if(is_string($data['data'])) return result($this->errorcode['dataError'],$data['data'],'data参数有误请检查');
        if(time() - $data['timestamp'] >60*5*1000) return result($this->errorcode['timeOut'],'','请求超时,请重新发送');
        
        $route = explode('/',strtolower($request->pathinfo()));
        $machineType = DB::table('dicMachineType')->where('class',$route[2])->value('code');

        //测试环境
        if(strstr($request->pathinfo(),'test/')){
            $integrator = DB::connect('integrator')->table('integrator_projectIntegrator')
            ->where('appId',$data['appId'])
            ->field(['appId','appSecret','corpCode','projectCode'])
            ->find();
            if(empty($integrator)) return result($this->errorcode['appidError'],'','appid错误');
            $integratorstatus = DB::table('integrator_status')->where([['machineType','like',"%".$machineType.'%'],['corpCode','=',$integrator['corpCode']]])->value('id');
            if($integratorstatus) return result(FAIL,'','该公司已通过此测试,请勿继续上传');
        }else{
            //正式环境
            $integrator = DB::table('integrator_projectIntegrator')
            ->alias('pi')
            ->join('integrator_projectMachineType pimt','pimt.projectIntegratorId = pi.id and  pimt.status=1')
            ->where(['pi.appId'=>$data['appId'],'pimt.machineType'=>$machineType])
            ->field(['appId','appSecret','corpCode','projectCode'])
            ->find();
            if(empty($integrator)) return result($this->errorcode['ruleError'],$integrator,'无权限访问该API');
        }
 
        $sign = $this->getSign($data,$integrator['appSecret']);
        if($sign != $data['sign']) dd($sign);
        if($sign != $data['sign']) return result($this->errorcode['signError'],'','签名错误');

        $request->data = $data['data'];
        $request->appSecret = $integrator['appSecret'];
        $request->prjCode = $integrator['projectCode'];
        $request->corpCode = $integrator['corpCode'];
        return $next($request);
    }
    /**
     * @Descripttion: 生成签名
     * @Author: Terry
     * @Date: 2021-07-18 16:18:23
     * @param {*}
     */
    private function getSign($data,$secret){
        unset($data['sign']);
        $data['data'] = json_encode($data);
        $sign = "appId=".$data['appId']."&appSecret=".$secret."&data=".$data['data']."&timestamp=".$data['timestamp'];
        $sign = strtolower($sign);
        return md5($sign);
    }
}
